%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Module summary
%%
%% Main authors: Olle
%% Progress to usefulness: 50%
%%
%% Library functions for communication between servers, 
%% using message passing. 
%% Simplifies sending, listening, querying etc.
%%
%% Currently general functions not bound to any specific protocol,
%% but places some requirements on protocols. 
%%
%%%%%%%%%%%%%%%%%%%%%%%

-module(serveriface).
-compile(export_all).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Node interaction support functions
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rem_query(From, To, Msg) ->
  To ! {From, Msg},
  receive
    {To, Reply} -> case Reply of
       {return, Val}  -> Val;
       {error, Err}   -> error("rem_query: Error reply: " + Err);
       _              -> error("rem_query: Invalid reply")
    end
  end.

rem_call(From, To, Msg) ->
  To ! {From, Msg},
  receive
    {To, Reply} -> case Reply of
       ok             -> ok;
       {error, Err}   -> error("rem_query: Error reply: " + Err);
       _              -> error("rem_query: Invalid reply")
    end
  end.

rem_return(From, To, Val) ->
  To ! {From, {return, Val}},
  ok.

rem_return_ok(From, To) ->
  To ! {From, ok}.

rem_return_error(From, To, Msg) ->
  To ! {From, {error, Msg}},
  ok.

